# -*- coding: utf-8 -*-
import scrapy
from scrapy_splash import SplashRequest
import os
from utils.splash_scripts import get_scripts
import json

class ZhihuSplashSpider(scrapy.Spider):
    name = 'zhihu_splash'
    allowed_domains = ['www.zhihu.com']

    # start_urls = ['http://https://www.zhihu.com/question/305244724/']

    def start_requests(self):
        script = get_scripts('detail_zhihu')
        from utils.cookies_get import make_cookie
        cookie = make_cookie()

        from test.settings import ZHIHU_COOKIE_FILE

        with open(ZHIHU_COOKIE_FILE, 'r') as f:
            cookies_json = f.read()

        cookies = json.loads(cookies_json)
        #cookie = {}
        headers = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language': 'en',
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
        }
        #headers = {}
        yield SplashRequest(headers=headers,cookies=cookies,url="https://www.zhihu.com/question/315244741", callback=self.parse, meta={'cookiejar': 1},args={'lua_source': script, 'timeout': 300}, endpoint='execute')
        #yield scrapy.Request(url='https://www.zhihu.com/question/35426484/', callback=self.test, cookies=cookie)

    def parse(self, response):
        html = response._body.decode()
        base_dir = os.path.dirname(os.path.abspath(__file__))
        with open(os.path.join(base_dir, 'save_file', 'zhihu_splash.html'), 'w') as f:
            f.write(html)
        x = response

        y = 1
        pass

    def test(self, response):
        html = response._body.decode()
        base_dir = os.path.dirname(os.path.abspath(__file__))
        with open(os.path.join(base_dir, 'save_file', 'zhihu_splash.html'), 'w') as f:
            f.write(html)
        headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
        }
        # headers = {
        #
        # }
        script = get_scripts('index_zhihu')
        cookie = response.request.cookies
        yield SplashRequest(headers=headers,cookies=cookie, url="https://www.zhihu.com", callback=self.parse, args={'lua_source': script, 'timeout': 300}, endpoint='execute')
